//#pragma once
#include"graphics.h"

//*************************************人机对战



extern WINDOW* g_win;
extern GRAPHICS_COLOR g_fore_color;
extern GRAPHICS_COLOR g_back_color;
extern int g_style;

extern int count;
extern POINT* old_head;
extern POINT* head;
POINT cursor = {9,22,NULL};          /////////////////////////////////////////////////////////////////////////////////
//extern POINT cursor;

void manAndComputer()
{	
	clear_screen(0,0,COLS,LINES);//清屏
	char* ptr2 ="欢乐五子棋";
	mvaddstr(3,33,ptr2);				//标题
       drawChessboard();
       dsphelp();					//初始化棋盘
	//POINT cursor = {15,40,NULL}; 			//定义一个光标初始化
	
	head->next = NULL;					//初始化
	count = 0;						//初始化
	drawCursor(cursor);					//初始化光标
	wrefresh(g_win);					//刷新窗口，显示窗口的最新变化
	
	int getc;
	while(1)
	{
		
		getc=readkey();
		switch(getc)
		{
			case KEYUP:
				if(cursor.x > X_START)
					(cursor.x)-=LENGTH;
                            else if(cursor.x=X_START)
                                   (cursor.x)=35;
				break;
			case KEYDOWN:
				if(cursor.x < X_END)
					(cursor.x)+=LENGTH;
                            else if(cursor.x=X_END)
                                   (cursor.x)=7;
				break;
			case KEYLEFT:
				if(cursor.y > Y_START)
					(cursor.y)-=WIDTH;
                           else if(cursor.y=Y_START)
                                   (cursor.y)=66;
				break;
			case KEYRIGHT:
				if(cursor.y < Y_END)
					(cursor.y)+=WIDTH;
                            else if(cursor.y=Y_END)
                                   (cursor.y)=10;
				break;
			default:
				if(getc == 'q')		//判断为悔棋
				{
					delChess(head);
					delChess(head);
				}
				else if(getc == ESC)		//判断为ESC退出人机对战
					return;
				else if(getc == SPACE)	//判断如果为空格下子
				{
					int tmp =count;
					addChess(cursor,head);
					drawChess(head);	//画棋子
					wrefresh(g_win);	//刷新窗口，显示窗口的最新变化
					if(judgeVictor(head))//判断输赢
					{
                                       old_head=head; 
						//POINT* tmp = head;
						//while(tmp != NULL)	//释放栈
						//{	
						//	tmp=tmp->next;
						//	free(head);
						//	head = tmp;
						//	
						//}
						return;
					}
					if(tmp != count)
					{
						computerStep(head);		
					}
					if(judgeVictor(head))
                                         {
                                          old_head=head; 
						return;
					}
				}
                            else if(getc=='r')
                               {  
                             //try  successfual!!!!............. 
                             //delChess(head);
		             //delChess(head);
                             computerStep(head);
                             if(judgeVictor(head))
			           {
                               old_head=head;
			          return;
                                 } 
                
                             clear_screen(0,0,COLS,LINES);		//清屏
		
		             drawChessboard();				//画棋盘
		             dsphelp();     
		             drawCursor(cursor);			       //画光标
		   
		             drawChess(head);				//画棋子
	
		             wrefresh(g_win);
                             sleep(0.5);                          
                          computerStep(head);
                            if(judgeVictor(head))
                                 {
                               old_head=head;
			          return;
                                 }   
                              }
			    break;
		}
		clear_screen(0,0,COLS,LINES);		//清屏
		
		drawChessboard();				//画棋盘
              dsphelp();
		
		drawCursor(cursor);				//画光标
		
		drawChess(head);				//画棋子
	
		wrefresh(g_win);				//刷新窗口，显示窗口的最新变化
		//judgeVictor(head);		
		
	}
}

